package cn.com.zsw.gblog.modules.user.controller;

import cn.com.zsw.gblog.config.cache.RedisCache;
import cn.com.zsw.gblog.constants.SysConstants;
import cn.com.zsw.gblog.modules.user.entity.GbUserEntity;
import cn.com.zsw.gblog.modules.user.entity.ThirdPartUserEntity;
import cn.com.zsw.gblog.modules.user.service.GbUserService;
import cn.com.zsw.gblog.modules.user.service.ThirdPartUserService;
import cn.com.zsw.gblog.utils.JwtUtils;
import cn.com.zsw.gblog.vo.R;
import cn.com.zsw.gblog.modules.user.vo.UserInfoVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author ：shiwangzhou
 * @Date ：Created in 2020-09-24 13:02
 * @Description：
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    ThirdPartUserService thirdPartUserService;
    @Autowired
    GbUserService userService;
    @Autowired
    RedisCache redisCache;

    /**
     * desc:
     * 1. 修改为不用登陆也可以访问,但是查询到的数据为空
     *
     * @param request 请求
     * @return cn.com.zsw.gblog.vo.R
     * @author: shiwangZhou
     * @date: 2020-10-07 15:50
     */
    @GetMapping("info")
    public R getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        if (StringUtils.isEmpty(token) || "anonymousUser".equalsIgnoreCase(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString())) {
            return R.SUCCESS.msg("当前用户未登录").data(null);
//            return R.status(ResponseCode.NOT_LOGIN.getCode()).msg("当前用户未登录").data(null);
        }
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        GbUserEntity gbUser = userService.getOne(Wrappers.<GbUserEntity>lambdaQuery().eq(GbUserEntity::getUsername, user.getUsername()));
        UserInfoVO userInfoVO = redisCache.getCacheObject(SysConstants.REDIS_GBLOG_USER_CACHE + gbUser.getId());
        return R.SUCCESS.data(userInfoVO).msg(null);
    }

    @GetMapping("third")
    public R thirdUserInfo() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        GbUserEntity gbUser = userService.getOne(Wrappers.<GbUserEntity>lambdaQuery().eq(GbUserEntity::getUsername, user.getUsername()));
        Assert.notNull(gbUser,"未查询到相关用户");
        final List<ThirdPartUserEntity> thirdPartUserEntities = thirdPartUserService.listByUserId(gbUser.getId());
        return R.SUCCESS.data(thirdPartUserEntities).msg(null);
    }
}
